package com.ry.demo.concurrent.demo.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;


public class ReentrantReadWriteLockDemo {

    public static void main(String[] args) throws InterruptedException {

        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

        lock.writeLock().lock();
        lock.writeLock().lock();

//        lock.readLock().lock();
//        lock.readLock().unlock();

        Thread.sleep(1000);
        new Thread(()->{
            lock.writeLock().lock();
            System.out.println("获取写锁成功");
        }).start();

        Thread.sleep(1000);
        new Thread(()->{
            lock.readLock().lock();
            System.out.println("获取读锁成功");
        }).start();

        lock.writeLock().unlock();
        lock.writeLock().unlock();

    }

    static class ReadReadLock {
        public static void main(String[] args) {
            ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
            lock.readLock().lock();
            lock.readLock().lock();
            lock.readLock().unlock();
        }
    }

}
